package wysiwyg.tools
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	
	import wysiwyg.layers.Layer;
	import wysiwyg.model.Model;
	import wysiwyg.model.ModelLocator;
	import wysiwyg.objectHandles.LineHandles;
	import wysiwyg.shapes.LineShape;

	public class LineTool extends ToolBase
	{
		private var theModel:Model=ModelLocator.getInstance().theModel;
		private var layersArray:ArrayCollection=theModel.layersArray;
		
		public function LineTool()
		{
			label="L";
		}

		override public function mouseDownHandler(event:MouseEvent):void
		{
			if(theModel.selectedLayers&&theModel.selectedLayers.length>0)
				currentLayer=theModel.selectedLayers.getItemAt(0) as Layer;
			else
			{
				currentLayer=new Layer();
				currentLayer.percentHeight=100;
				currentLayer.percentWidth=100;
				theModel.workspace.addChild(currentLayer);
				layersArray.addItemAt(currentLayer,0);
			}
			shape = new LineShape();
			currentLayer.addChild(shape);
			((shape as LineShape).shapeHandles as LineHandles).startPoint=new Point(theModel.workspace.mouseX,theModel.workspace.mouseY);
			((shape as LineShape).shapeHandles as LineHandles).startPoint=new Point(theModel.workspace.mouseX,theModel.workspace.mouseY);
			shape.invalidateDisplayList();
		}
		
		override public function mouseUpHandler(event:MouseEvent):void
		{
			((shape as LineShape).shapeHandles as LineHandles).endPoint=new Point(theModel.workspace.mouseX,theModel.workspace.mouseY);
			((shape as LineShape).shapeHandles as LineHandles).endPoint=new Point(theModel.workspace.mouseX,theModel.workspace.mouseY);
			((shape as LineShape).shapeHandles as LineHandles).invalidateDisplayList();
			shape.invalidateDisplayList();
			shape.select();
			shape=null;
		}
		
		override public function mouseMoveHandler(event:MouseEvent):void
		{
			if(shape)
			{
				((shape as LineShape).shapeHandles as LineHandles).endPoint=new Point(theModel.workspace.mouseX,theModel.workspace.mouseY);
				shape.invalidateDisplayList();
				
			}
		}
		
	}
}